package com.yixun.qfbao.response.premises.detail.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;

import com.yixun.qfbao.response.premises.detail.UrlInfo;
import com.yixun.qfbao.response.premises.detail.util.resolveLabel.ResolveLabelUtilFactory;

/**
 * @author zilong.liu
 * @date 2020-9-9
 */
public class SplitHtmlUtil {

	//控制调用方法递归次数为10
	private int cycleTimes = 10;
	
	//递归方法次数的原始记录
	private int cycleTimesRecord = 10;
	
	/**
	 * 根据传入的片段，移除从start到end开始之间的字符
	 * 	设置标志位，最多只允许递归10次，避免因为脏数据而导致的无限递归
	 * @param input
	 */
	public String removeStringFromInput(String input, String start, String end) {
		if(StringUtils.isEmpty(input)) {
			return null;
		}
		int startIndex = input.indexOf(start);
		if(startIndex > -1 && cycleTimes > 0) {
			String prefix = input.substring(0, startIndex);
			String subffix = input.substring(startIndex);
			int endIndex = subffix.indexOf(end);
			int endLength = end.length();
			String newHtml = prefix + subffix.substring(endIndex + endLength);
			cycleTimes --;
			return removeStringFromInput(newHtml, start, end);
		}
		cycleTimes = cycleTimesRecord;
		return input;
	}
	
	/**
	 * 根据传入的片段，移除从start到end开始之间的字符
	 * 	设置标志位，最多只允许递归10次，避免因为脏数据而导致的无限递归
	 * @param input
	 * @param removeList 存放截取的集合
	 */
	public String removeStringFromInput(String input, String start, String end, List<String> removeList) {
		int startIndex = input.indexOf(start);
		if(startIndex > -1 && cycleTimes > 0) {
			String prefix = input.substring(0, startIndex);
			String subffix = input.substring(startIndex);
			int endIndex = subffix.indexOf(end);
			int endLength = end.length();
			removeList.add(subffix.substring(0, endIndex + endLength));
			String newHtml = prefix + subffix.substring(endIndex + endLength);
			cycleTimes --;
			return removeStringFromInput(newHtml, start, end, removeList);
		}
		cycleTimes = cycleTimesRecord;
		return input;
	}
	
	/**
	 * 有些数据中涉及了特殊的格式，对格式进行特殊处理
	 * @param urlInfoList
	 */
	public static void getRealUrlInfoList(List<UrlInfo> urlInfoList){
		if(CollectionUtils.isEmpty(urlInfoList)) {
			return;
		}
		for (UrlInfo urlInfo : urlInfoList) {
			urlInfo.setUrl(getRealUrlInfo(urlInfo.getUrl()));
		}
	}
	
	// 过滤规则，再次截取，获得。如果是http开头，则截取到第一个 "
	private static String getRealUrlInfo(String urlInfo) {
		String realUrlInfo = urlInfo;
		if(urlInfo.startsWith("http")) {
			int index = urlInfo.indexOf("\"");
			if(index > -1) {
				realUrlInfo = urlInfo.substring(0, index);
			}
		}
		return realUrlInfo;
	}
	

	public void setCycleTimes(int cycleTimes) {
		this.cycleTimes = cycleTimes;
		this.cycleTimesRecord = cycleTimes;
	}
	
	private static final String[] EXCLUSION_LABELS = new String[] {"<img", ">", 
			"<iframe class=\"ql-video\"", "</iframe>"};
	private static final String[] LABEL_ATTRS = new String[] {"1", "src=\"", "\">", "2", "src=\"", "\">"};
	
	/**
	 * 按照截取规则截取对应的内容
	 * @param html
	 * @return
	 */
	public Map<String, Object> splitHtml(String html) {
		Map<String, Object> resMap = new HashMap<>();
		if(html == null) {
			return resMap;
		}
		String content = html;
		List<UrlInfo> urlInfoList = new ArrayList<>();
		for(int i = 0; i < EXCLUSION_LABELS.length; i += 2) {
			List<String> labelList = new ArrayList<>();
			content = removeStringFromInput(content, EXCLUSION_LABELS[i], 
					EXCLUSION_LABELS[i+1], labelList);
			for (String label : labelList) {
				UrlInfo urlInfo = new UrlInfo();
				urlInfo.setType(LABEL_ATTRS[i + i/2]);
				urlInfo.setCoverimageUrl("");
				urlInfo.setUrl(transferUrl(ResolveLabelUtilFactory.getResolveLabelUtil(LABEL_ATTRS[i + i/2]).
						getAttrFromLabel(label, LABEL_ATTRS[i + i/2 + 1], LABEL_ATTRS[i + i/2 + 2])));
				urlInfoList.add(urlInfo);
			}
		}
		resMap.put("content", content);
		resMap.put("urlInfoList", urlInfoList.size() == 0? null: urlInfoList);
		return resMap;
	}
	
	/**
	 * 替换转义字符
	 * @return
	 */
	private static String transferUrl(String url) {
		url = url.replaceAll("&amp;", "&");
		url = url.replaceAll("&lt;", "<");
		url = url.replaceAll("&gt;;", ">");
		return url;
	}
//	public static void main(String[] args) {
//		String html = "12312312312<img src=\"http://imgch.qfstatic.com/upload/default/20200908/1599536735818.png\">"
//				+ "567865687<img src=\"http://imgch.qfstatic.com/upload/default/20200908/1599536735818.png\">哈哈哈"
//				+ "视频<iframe class=\"ql-video\" frameborder=\"0\" allowfullscreen=\"true\" src=\"https://v.qq.com/x/page/x0635pooz7t.html\"></iframe>";
//		PremisesDynamicResDto premisesDynamicResDto = new PremisesDynamicResDto();
//		premisesDynamicResDto.splitHtml(html);
//		System.out.println(123);
//	}

	public static void main(String[] args) {
		//	
		SplitHtmlUtil splitHtmlUtil = new SplitHtmlUtil();
		String html = "12312312312<img src=\"http://imgch.qfstatic.com/upload/default/20200908/1599536735818.png\">"
				+ "567865687<img src=\\\"http://imgch.qfstatic.com/upload/default/20200908/1599536735818.png\\\">哈哈哈"
				+ "视频<iframe class=\"ql-video\" frameborder=\"0\" allowfullscreen=\"true\" src=\"https://v.qq.com/x/page/x0635pooz7t.html\"></iframe>";
		List<String> a = new ArrayList<>();
		System.out.println(splitHtmlUtil.removeStringFromInput(html, "<img", ">", a));
		System.out.println(a);
	}
}
